package App.Config;

import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.concurrent.TimeUnit;

/**
 * Created by xw on 2019/1/23.
 */
@org.springframework.context.annotation.Configuration
public class SourceConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * @Description: 对文件的路径进行配置,创建一个虚拟路径/file/** ，
         * 即只要在<img src="/file/images/20180522/9aa64b2b-a558-421e-929c-537ff0aecdba.jpg" />
         * 便可以直接引用图片
         *这是图片的物理路径 "file:/+本地图片的地址"
         * @Date： Create in 14:08 2017/12/20
         *
         */
        //读取配置文件中的上传路径

        String _filePath = "/data/approot/uploads/";
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {
            _filePath = "E:\\uploads\\";
        }

        registry.addResourceHandler("/file/**").addResourceLocations("file:"+_filePath).setCacheControl(CacheControl.maxAge(48, TimeUnit.HOURS).cachePublic());

                //.setCachePeriod(3600*24*2);


        super.addResourceHandlers(registry);
    }
}